package com.company.project.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.sys.entity.BookEntity;
import com.company.project.sys.entity.SysUser;
import com.company.project.sys.entity.UserBookEntity;
import com.company.project.sys.service.BookService;
import com.company.project.sys.service.UserBookService;
import com.company.project.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import com.company.project.common.utils.DataResult;


/**
 * 
 *
 * @author ZYF
 * @date 2021-05-20 10:20:50
 */
@Controller
@RequestMapping("/")
public class UserBookController {
    @Autowired
    private UserBookService userBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;


    @ApiOperation(value = "新增")
    @ResponseBody
    public DataResult add(@RequestBody UserBookEntity userBook){
        userBookService.save(userBook);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        userBookService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @ResponseBody
    public DataResult update(@RequestBody UserBookEntity userBook){
        userBookService.updateById(userBook);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("userBook/listByPage")
    @ResponseBody
    public DataResult findListByPage(@RequestBody UserBookEntity userBook){
        Page page = new Page(userBook.getPage(), userBook.getLimit());
        LambdaQueryWrapper<UserBookEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(UserBookEntity::getId, userBook.getId());
        IPage<UserBookEntity> iPage =userBookService.page(page, queryWrapper);
        for(UserBookEntity userBookVo : iPage.getRecords()) {
            SysUser sysUser =   userService.getById(userBookVo.getUserId());;
            if(null !=sysUser) {
                userBookVo.setSysUser(sysUser);
            }
            BookEntity book = bookService.getById(userBookVo.getBookId());
            if(null != book){
                userBookVo.setBook(book);
            }
        }
        return DataResult.success(iPage);
    }

    @GetMapping("userBook/info")
    @ResponseBody
    public DataResult userBook(String id) {
        UserBookEntity userBookEntity = userBookService.getById(id);
        return DataResult.success(userBookEntity);
    }

}
